{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "from pymilvus import (\n",
    "    MilvusClient,\n",
    "    DataType\n",
    ")\n",
    "dim=512\n",
    "collection_name='yyy'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "milvus_client = MilvusClient(\"http://localhost:19530\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "has_collection = milvus_client.has_collection(collection_name, timeout=5)\n",
    "if has_collection:\n",
    "    milvus_client.drop_collection(collection_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'auto_id': False, 'description': '', 'fields': [{'name': 'id', 'description': '', 'type': <DataType.INT64: 5>, 'is_primary': True, 'auto_id': False}, {'name': 'embeddings', 'description': '', 'type': <DataType.FLOAT_VECTOR: 101>, 'params': {'dim': 512}}, {'name': 'title', 'description': '', 'type': <DataType.VARCHAR: 21>, 'params': {'max_length': 64}}], 'enable_dynamic_field': True}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "schema = milvus_client.create_schema(enable_dynamic_field=True)\n",
    "schema.add_field(\"id\", DataType.INT64, is_primary=True)\n",
    "schema.add_field(\"embeddings\", DataType.FLOAT_VECTOR, dim=dim)\n",
    "schema.add_field(\"title\", DataType.VARCHAR, max_length=64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_params = milvus_client.prepare_index_params()\n",
    "index_params.add_index(field_name = \"embeddings\", metric_type=\"L2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "milvus_client.create_collection(collection_name, schema=schema, index_params=index_params, consistency_level=\"Strong\",)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "rng = np.random.default_rng(seed=19530)\n",
    "rows = [\n",
    "        {\"id\": 1, \"embeddings\": rng.random((1, dim))[0], \"a\": 100, \"title\": \"t1\"},\n",
    "        {\"id\": 2, \"embeddings\": rng.random((1, dim))[0], \"b\": 200, \"title\": \"t2\"},\n",
    "        {\"id\": 3, \"embeddings\": rng.random((1, dim))[0], \"c\": 300, \"title\": \"t3\"},\n",
    "        {\"id\": 4, \"embeddings\": rng.random((1, dim))[0], \"d\": 400, \"title\": \"t4\"},\n",
    "        {\"id\": 5, \"embeddings\": rng.random((1, dim))[0], \"e\": 500, \"title\": \"t5\"},\n",
    "        {\"id\": 6, \"embeddings\": rng.random((1, dim))[0], \"f\": 600, \"title\": \"t6\"},\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'insert_count': 6, 'ids': [1, 2, 3, 4, 5, 6], 'cost': 0}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "milvus_client.insert(collection_name, rows)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "danwen1",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
